/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telefonia.dao;

import conexion.BD;
import conexion.SqlFormat;
import telefonia.entity.Region;
import telefonia.entity.Pais;

/**
 *
 * @author Duoc
 */
public class DaoRegion {
    
    
    public static boolean sqlInsert(Region region)
    {
        SqlFormat sql = new SqlFormat(SqlFormat.INSERTAR, "region");
        sql.addField("id", region.getIdRegion());
        sql.addField("descripcion", region.getDescripcion());
        sql.addField("idPais", region.getPais().getCodigo());
      
        BD.getInstance().sqlEjecutar(sql.crearInstruccion());
     
        
        return true;
    }
    public static boolean sqlUpdate(Region region)
    {
       
        SqlFormat sql = new SqlFormat(SqlFormat.ACTUALIZAR, "region");
        sql.where("id", region.getIdRegion());
        sql.addField("descripcion", region.getDescripcion());
        sql.addField("idPais", region.getPais().getCodigo());
        
        BD.getInstance().sqlEjecutar(sql.crearInstruccion());
        
        return true;
    }
    public static boolean sqlDelete(Region region)
    {
        SqlFormat sql = new SqlFormat(SqlFormat.ELIMINAR,"region");
        sql.where("id",region.getIdRegion());
        
        System.out.println(sql.crearInstruccion());
        BD.getInstance().sqlEjecutar(sql.crearInstruccion());
        
        
        return true;
    }
    public static Region sqlRead(Region region)
    {
        SqlFormat sql = new SqlFormat(SqlFormat.SELECCIONAR, "region");
        sql.where("id", region.getIdRegion());
        
        BD.getInstance().sqlQuery(sql.crearInstruccion());
        if(BD.getInstance().sqlFetch())
            return new Region(BD.getInstance().getInt("id"),
                    BD.getInstance().getString("descripcion"), 
                    new Pais(BD.getInstance().getString("idPais")));
        
        return null;//Si no hay nada se devuelve null
        
    }
    
}
